package com.admin.dao.adminlogindao;

import com.admin.dto.QueryReturnListBean;
import com.pojo.AdminBean;
import com.pojo.BorInfoBean;
import com.pojo.BorrowerBean;
import com.utils.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class AdminDaoImpl implements  AdminDaoInf{

    /**
     * 管理员的增，删，查，改的具体实现类
     */
    @Override
    public boolean adminAdd(AdminBean ab) {
        return false;
    }

    @Override
    public int adminUpdate(AdminBean ab) {
        return 0;
    }


    @Override
    public Boolean adminRegisterCheck(String sql,Connection conn,AdminBean ab) throws SQLException{
        boolean bool=false;
        PreparedStatement pstm=null;

        ResultSet rs=null;

        try {
            pstm=conn.prepareStatement(sql);
            pstm.setString(1,ab.getAdminName());
            pstm.setString(2,ab.getAdminPwd());
            rs = pstm.executeQuery();
            if (rs.next()){
                bool=true;
            }
        } finally {
            if (pstm!=null){
                pstm.close();
            }

            if (rs!=null){
                rs.close();
            }
        }
        return bool;
    }


    @Override
    public Boolean adminLoginCheck(String sql,Connection conn,AdminBean ab) throws SQLException{
        boolean bool=false;
        PreparedStatement pstm=null;

        ResultSet rs=null;

        try {
            pstm=conn.prepareStatement(sql);
            pstm.setString(1,ab.getAdminName());
            pstm.setString(2,ab.getAdminPwd());
            rs = pstm.executeQuery();
            if (rs.next()){
               bool=true;
            }
        } finally {
            if (pstm!=null){
                pstm.close();
            }

            if (rs!=null){
                rs.close();
            }
        }
        return bool;
    }

    /**
     * 注册管理员
     * @param sql
     * @param conn
     * @param ab
     * @return
     */
    @Override
    public boolean adminRegister(String sql, Connection conn, AdminBean ab)throws SQLException {
        boolean bool=false;
        PreparedStatement pstm=null;
        ResultSet rs=null;
        try {
            pstm=conn.prepareStatement(sql);
            pstm.setString(1,ab.getAdminName());
            pstm.setString(2,ab.getAdminPwd());
            pstm.setString(3,ab.getAdminPhone());
            pstm.setLong(4,ab.getAdminLevel());
            int i = pstm.executeUpdate();
            if (i>0){
                bool=true;
            }
        } finally {
            if (pstm!=null){
                pstm.close();
            }
            if (rs!=null){
                rs.close();
            }
        }
        return bool;
    }


   public AdminBean getAdminAllInfoDao(String sql, Connection conn, String name)throws SQLException{
       PreparedStatement pstm=null;
       AdminBean adminBean = new AdminBean();
       ResultSet rs=null;

       try {
           pstm=conn.prepareStatement(sql);
           pstm.setString(1,name);

           rs = pstm.executeQuery();
           if (rs.next()){
               adminBean.setCreateTime(rs.getString("create_time"));

               adminBean.setAdminName(rs.getString("adminname"));
               adminBean.setAdminPwd(rs.getString("adminpwd"));
               adminBean.setAdminPhone(rs.getString("adminphone"));
               adminBean.setAdminLevel(rs.getInt("adminlevel"));
               adminBean.setEmail(rs.getString("email"));
           }
       } finally {
           if (pstm!=null){
               pstm.close();
           }

           if (rs!=null){
               rs.close();
           }
       }
       return adminBean;
    }

    @Override
    public boolean returnBookById(String sql, Connection conn, String bookId, String idCard)throws SQLException  {
        boolean bool=false;
        PreparedStatement pstm=null;
        ResultSet rs=null;
        try {
            pstm=conn.prepareStatement(sql);
            pstm.setString(1,bookId);
            pstm.setString(2,idCard);
            int i = pstm.executeUpdate();
            if (i>0){
                bool=true;
            }
        } finally {
            if (pstm!=null){
                pstm.close();
            }
            if (rs!=null){
                rs.close();
            }
        }
        return bool;
    }

    @Override
    public boolean addLendInfo(String sql, Connection conn, BorInfoBean borInfoBean) throws SQLException{
        boolean bool=false;
        PreparedStatement pstm=null;
        ResultSet rs=null;
        try {
            pstm=conn.prepareStatement(sql);
            pstm.setLong(1,borInfoBean.getBorId());
            pstm.setLong(2,borInfoBean.getBookId());
            pstm.setString(3,borInfoBean.getRepayDate());
            pstm.setDouble(4,borInfoBean.getPrice());
            pstm.setString(5,borInfoBean.getState());
            int i = pstm.executeUpdate();
            if (i>0){
                bool=true;
            }
        } finally {
            if (pstm!=null){
                pstm.close();
            }
            if (rs!=null){
                rs.close();
            }
        }
        return bool;
    }

    @Override
    public BorrowerBean queryUserIdByIdCard(String sql, Connection conn, String idCard) throws SQLException {
        PreparedStatement pstm=null;
        ResultSet rs=null;
        BorrowerBean borrowerBean = new BorrowerBean();
        try {
            pstm=conn.prepareStatement(sql);
            pstm.setString(1,idCard);
            rs = pstm.executeQuery();
            if (rs.next()){
                borrowerBean.setUserId(rs.getLong("userid"));
            }
        } finally {
            if (pstm!=null){
                pstm.close();
            }
            if (rs!=null){
                rs.close();
            }
        }
        return borrowerBean;
    }

    @Override
    public boolean reduceUserStockAndBalance(String sql, Connection conn, long userId, double borrowPrice)throws SQLException  {
        boolean bool=false;
        PreparedStatement pstm=null;
        ResultSet rs=null;
        try {
            pstm=conn.prepareStatement(sql);
            pstm.setDouble(1,borrowPrice);
            pstm.setLong(2,userId);
            int i = pstm.executeUpdate();
            if (i>0){
                bool=true;
            }
        } finally {
            if (pstm!=null){
                pstm.close();
            }
            if (rs!=null){
                rs.close();
            }
        }
        return bool;
    }

}
